//
//  NortUSAViewController.m
//  HWSGW
//
//  Created by avachen on 15/10/31.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//
/*
 电影{
 title 标题
 image 图片
 year 年份
 rating 评分
 }
 */

#import "NortUSAViewController.h"
#import "Movie.h"
#import "USMovieCell.h"
#import "HWDataService.h"
#import "PosterView.h"

@interface NortUSAViewController ()

@end

@implementation NortUSAViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1、初始化导航栏上的按钮
    [self _loadNavigationItem];
    //2、初始化列表视图
    [self _loadTableView];
    //3、创建海报视图
    [self _loadPosterView];
    //4、解析数据
    [self _requestData];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
}
//4、解析数据
- (void)_requestData{
    //json数据的解析
    //json数据解析的工具
    //JSONKit\TouchJSON\SBJSON ,开源的json数据解析工具
    
    //NSJSONSerialization
    NSDictionary *jsonDic = [HWDataService requestData:us_box];
//    NSLog(@"%@", dic);
    _data = [[NSMutableArray alloc] initWithCapacity:5];
    
    NSArray *array = [jsonDic objectForKey:@"subjects"];
    //遍历解析json数据所取得的数组
    for (NSDictionary *dic in array) {
        NSDictionary *subject = [dic objectForKey:@"subject"];
        //创建movie对象
        Movie *movie = [[Movie alloc] init];
        //把解析出来的数据存到model对象里
        movie.average = [[subject objectForKey:@"rating"] objectForKey:@"average"];
        movie.collect_count = [subject objectForKey:@"collect_count"];
        movie.images = [subject objectForKey:@"images"];
        movie.movieID = [subject objectForKey:@"id"];
        movie.title = [subject objectForKey:@"title"];
        movie.subtype = [subject objectForKey:@"subtype"];
        movie.year = [subject objectForKey:@"year"];
        movie.original_title = [subject objectForKey:@"original_title"];
        
        //将movie对象存到数组中
        [_data addObject:movie];
        
    }
    //刷新列表
    [_tableView reloadData];
    
    [_posterView setData:_data];

}
//3、创建海报视图
- (void)_loadPosterView{
    _posterView = [[PosterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49)];
//    _posterView.backgroundColor = [UIColor clearColor];
    _posterView.hidden = NO;
    _posterView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    
    [self.view insertSubview:_posterView atIndex:0];
    
}
//2、初始化列表视图
- (void)_loadTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 49 + 64)];
    _tableView.rowHeight = 120;
    _tableView.separatorColor = [UIColor darkGrayColor];
    _tableView.hidden = YES;
    
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    
    [self.view addSubview:_tableView];
    
}
#pragma mark - UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"movieCell";
    USMovieCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil) {
        //创建cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"USMovieCell" owner:self options:nil]lastObject];
        
    }
    cell.movie = _data[indexPath.row];
    
    return cell;
}
//1、初始化导航栏上的按钮
- (void)_loadNavigationItem{
    //1、创建翻转的父视图
    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    //2、创建右侧2个翻转按钮
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.tag = 201;
    [button2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    button2.hidden = NO;
    [button2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    button2.frame = buttonView.bounds;
    [button2 addTarget:self action:@selector(rightBarClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView addSubview:button2];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.tag = 200;
    [button1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [button1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    button1.frame = buttonView.bounds;
    button1.hidden = YES;
     
    [button1 addTarget:self action:@selector(rightBarClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView addSubview:button1];
    
    
    //把父视图给页面右边按钮
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
- (void)rightBarClickAction:(UIButton *)button{
    //1、通过button拿父视图
    //button.superview
    //2、通过拿rightBarButtonItem
    UIView *buttonView = self.navigationItem.rightBarButtonItem.customView;
    //通过父视图拿到button1和button2
    UIButton *button1 = (UIButton *)[buttonView viewWithTag:200];
    UIButton *button2 = (UIButton *)[buttonView viewWithTag:201];
    button1.hidden = !button1.hidden;
    button2.hidden = !button2.hidden;
    
    [self flipView:buttonView left:button1.hidden];
    
    //3、切换翻转列表视图、海报视图的显示
    _tableView.hidden = !_tableView.hidden;
    _posterView.hidden = !_posterView.hidden;
    [self flipView:self.view left:button1.hidden];
    
    
    
}
- (void)flipView:(UIView *)supView left:(BOOL)flag{
    UIViewAnimationOptions flip =  flag ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight;
    
    [UIView transitionWithView:supView duration:0.5 options:flip
                    animations:^{
                        [supView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
                        
                    } completion:NULL];
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
